package com.aaa.project.controller;


import com.aaa.entity.ERole;
import com.aaa.project.service.IERoleService;
import com.aaa.util.PageInfo;
import com.aaa.util.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yuyongli
 * @since 2022-12-28
 */
@RestController
@RequestMapping("/e-role")
@Api(tags="角色信息api")
public class ERoleController {

    @Resource
    private IERoleService roleService;

    // 查询所有的role
   @PostMapping
   @ApiOperation("查询角色的信息")
    public Result getAllRole(@ModelAttribute PageInfo page,@ModelAttribute ERole role){
       QueryWrapper<ERole> queryWrapper = new QueryWrapper();
       queryWrapper.eq("is_deleted",0);
       if (role.getStatus() != null){
           queryWrapper.eq("status",role.getStatus());
       }
       if (role.getRoleName() != null){
           queryWrapper.like("role_name",role.getRoleName());
       }
       List<ERole> list = roleService.list(queryWrapper);
       System.out.println("list = " + list);
       return new Result(list);
    }

    /**
     * 添加角色的信息
     *
     */
    @PostMapping("addRole")
    @ApiOperation("添加角色的信息")
    public Result addRole(@ModelAttribute ERole role){
        // saveOrUpdate  添加或者是修改
        // role 对象有id的值的时候 修改
        // id 没有值的时候执行的是添加
        return new Result(roleService.saveOrUpdate(role));
    }


    @PostMapping("upStatus/{id}/{status}")
    @ApiOperation("修改角色的信息")
    public Result updRole(@PathVariable Integer id,@PathVariable String status){
        ERole eRole = new ERole();
        eRole.setId(id);
        eRole.setStatus(status);
        //boolean update = roleService.update(updateWrapper);
        return new Result(roleService.updateById(eRole));
    }

    /**
     * 删除角色的信息
     */
    @DeleteMapping("{id}")
    @ApiOperation("根据id删除角色的信息")
    public Result delRole(@PathVariable @ApiParam("角色的id") Integer id){
        return new Result(roleService.removeById(id));
    }
    /**
     * 权限分配的时候 使用的数据
     */
    @ApiOperation("根据角色的id修改角色的权限")
    @PostMapping("selectPermission/{id}")
    public Result selPermission(@PathVariable @ApiParam("角色的id") Integer id){
        return new Result(roleService.getPermissionData(id));
    }
}

